//
//  YeeShopDetailSubsVC.m
//  YeeShopProject
//
//  Created by CoderYee on 2017/3/6.
//  Copyright © 2017年 CoderYee. All rights reserved.
//

#import "YeeShopDetailSubsVC.h"
#import "XRCommonSegMentView.h"
#import "YeeShopInfoVC.h"
@interface YeeShopDetailSubsVC ()<UIPageViewControllerDelegate,UIPageViewControllerDataSource>
{
   // UIPageViewController   *_pageVC;
    XRCommonSegMentView    *_segMentView;
    
}
@property (strong, nonatomic)UIPageViewController *pageVC;
@property (strong, nonatomic)NSMutableArray *viewControllers;
@end

@implementation YeeShopDetailSubsVC

- (void)viewDidLoad
{
    [super viewDidLoad];
}
-(void)addOwnViews
{
    [super addOwnViews];
    [self addsegMentView];
    [self.view setBackgroundColor:xBackgroundColor];
    [self.view addSubview:self.pageVC.view];
}
-(void)addNavbarView
{
    [super addNavbarView];
}
-(void)configOwnViews
{
    [super configOwnViews];
}
-(void)addsegMentView
{
    _segMentView=[[XRCommonSegMentView alloc] initWithFrame:CGRectMake(0, 0, kMainScreenWidth, 40) titleArray:@[@"图文详情",@"产品参数",@"售后服务",@"推家店家"]];
    [_segMentView setBackgroundColor:kWhiteColor];
    [self.view addSubview:_segMentView];
}
// 根据数组元素，得到下标值
- (NSUInteger)indexOfViewController:(UIViewController *)viewControlller {
    return [self.viewControllers indexOfObject:viewControlller];
}
#pragma mark - UIPageViewControllerDataSource
// 返回下一个ViewController对象
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    
    NSUInteger index = [self indexOfViewController:viewController];
    
    if (index == NSNotFound) {
        return nil;
    }
    index++;
    if (index == [self.viewControllers count]) {
        return nil;
    }
    
    return self.viewControllers[index];
}
// 返回上一个ViewController对象
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    
    NSUInteger index = [self indexOfViewController:viewController];
    
    if (index == 0 || index == NSNotFound) {
        return nil;
    }
    index--;
    return self.viewControllers[index];
}
#pragma mark - UIPageViewControllerDelegate
// 翻页完成调用
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed
{
    UIViewController *vc = [pageViewController.viewControllers lastObject];
    
    NSInteger indx = [self indexOfViewController:vc];
    [_segMentView setLineViewFrameWithClickIndex:indx];
}
- (UIPageViewController *)pageVC {
    if (!_pageVC) {
        // 设置UIPageViewController的配置项
        NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:UIPageViewControllerSpineLocationNone] forKey:UIPageViewControllerOptionSpineLocationKey];
        
        _pageVC = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];
        _pageVC.dataSource = self;
        _pageVC.delegate = self;
        _pageVC.doubleSided=NO;
        _pageVC.view.frame = CGRectMake(0, 40, kMainScreenWidth, kMainScreenHeight-64);
        // 要显示的第几页
        NSArray *vcs = [NSArray arrayWithObject:self.viewControllers[0]];
        
        [_pageVC setViewControllers:vcs direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];
    }
    return _pageVC;
}
-(NSMutableArray*)viewControllers
{
    if (!_viewControllers)
    {
        _viewControllers=[[NSMutableArray alloc] init];
        YeeShopInfoVC *chargeVC=[[YeeShopInfoVC alloc]init];
        chargeVC.view.frame=self.pageVC.view.bounds;
        chargeVC.detailSubsVC=self;
        [self congtabelView:chargeVC.tableView];
        YeeShopInfoVC *courseVC=[[YeeShopInfoVC alloc]init];
        YeeShopInfoVC *chargeVC2=[[YeeShopInfoVC alloc]init];
        YeeShopInfoVC *courseVC2=[[YeeShopInfoVC alloc]init];
        [_viewControllers addObject:chargeVC];
        [_viewControllers addObject:courseVC];
        [_viewControllers addObject:chargeVC2];
        [_viewControllers addObject:courseVC2];
    }
    return _viewControllers;
}
-(void)congtabelView:(UITableView *)tableView
{
//   MJRefreshStateHeader * header = (MJRefreshStateHeader*)tableView.mj_header;
//    header.mj_y = - 94;
}
-(void)configTableViewDidScrollView:(UITableView *)tableView
{
    //改变headerView的frame
    [self.view bringSubviewToFront:_segMentView];
    NSLog(@"%f",tableView.contentOffset.y);
    if (tableView.contentOffset.y<0)
    {
      [_segMentView setFrame:CGRectMake(0, -tableView.contentOffset.y, kMainScreenWidth, 40)];
      //[self.pageVC.view setFrame:CGRectMake(0, -tableView.contentOffset.y, kMainScreenWidth, kMainScreenHeight-40)];
    }else
    {
        [_segMentView setFrame:CGRectMake(0,0, kMainScreenWidth, 40)];
       // [self.pageVC.view setFrame:CGRectMake(0, 40, kMainScreenWidth, kMainScreenHeight-64)];


    }
    
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}
@end
